package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.*;
import com.atguigu.jxc.domain.PurchaseCountVo;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.PurchaseListService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class PurchaseListServiceImpl implements PurchaseListService {
    @Autowired
    private PurchaseListDao purchaseListdao;
    @Autowired
    private PurchaseListGoodsDao purchaseListGoodsdao;
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private SupplierDao supplierDao;


    @Override
    @Transactional
    public ServiceVO<PurchaseList> savePurchaseListAndGoods(PurchaseList purchaseList, String purchaseNumber, String purchaseListGoodsStr) {
        User currentUser = userDao.findUserByName((String) SecurityUtils.getSubject().getPrincipal());
        Gson gson = new Gson();
        List<PurchaseListGoods> purchaseListGoodsList = gson.fromJson(purchaseListGoodsStr, new TypeToken<List<PurchaseListGoods>>() {
        }.getType());
        purchaseList.setUserId(currentUser.getUserId());
        purchaseList.setPurchaseNumber(purchaseNumber);
        purchaseListdao.insertPurchaseList(purchaseList);
        PurchaseList newPurchaseList =purchaseListdao.selectPurchaseListByNumber(purchaseNumber);
        Integer purchaseListId = newPurchaseList.getPurchaseListId();
        for (PurchaseListGoods purchaseListGoods : purchaseListGoodsList) {
            purchaseListGoods.setPurchaseListId(purchaseListId);
            purchaseListGoodsdao.insertPurchaseListGoods(purchaseListGoods);
            Goods goods = goodsDao.findByGoodsId(purchaseListGoods.getGoodsId());
            goods.setInventoryQuantity(goods.getInventoryQuantity()+purchaseListGoods.getGoodsNum());
            goodsDao.updateGoods(goods);
        }
        ServiceVO<PurchaseList> serviceVO = new ServiceVO<>(100, "请求成功", newPurchaseList);
        return serviceVO;
    }

    @Override
    public List<PurchaseList>  getPurchaseList(String purchaseNumber, Integer supplierId, Integer state, String sTime, String eTime) {
        List<PurchaseList> purchaseList = purchaseListdao.selectPurchaseList(purchaseNumber,supplierId,state,sTime,eTime);
        //System.out.println("purchaseList = " + purchaseList);
        for (PurchaseList list : purchaseList) {
            User user = userDao.getUserById(list.getUserId());
            Supplier supplier = supplierDao.getSupplierById(list.getSupplierId());
            list.setTrueName(user.getTrueName());
            list.setSupplierName(supplier.getSupplierName());
        }

        return purchaseList;
    }
    //查询进货单商品信息列表
    @Override
    public List<PurchaseListGoods> getPurchaseListGoodsList(Integer purchaseListId) {
        List<PurchaseListGoods> purchaseListGoodsList =  purchaseListGoodsdao.selectPurchaseListGoodsList(purchaseListId);
        return purchaseListGoodsList;
    }
    //进货单删除 并删除进货单对应商品
    @Override
    @Transactional
    public ServiceVO deletePurchaseList(Integer purchaseListId) {
        purchaseListGoodsdao.removepurchaseListGoods(purchaseListId);
        purchaseListdao.removePurchaseList(purchaseListId);
        ServiceVO serviceVO = new ServiceVO(100, "删除成功", null);
        return serviceVO;
    }
    //1.1、支付结算（修改进货单付款状态）
    // 将 state 改成1（已付或已退）  将paid金额 改成paybale 金额（已付金额改为 应付金额）
    @Override
    public ServiceVO updateStatePurchaseList(Integer purchaseListId) {
        purchaseListdao.updatePurchaseList(purchaseListId);
        ServiceVO serviceVO = new ServiceVO(100, "结算成功", null);
        return serviceVO;
    }
    //3.1、进货统计（可根据 商品类别、商品编码或名称 条件查询）
    @Override
    public String countPurchaseListGoodsList(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
        List<PurchaseCountVo> list = purchaseListdao.countPurchaseListGoodsList(sTime,eTime,goodsTypeId,codeOrName);
        Gson gson = new Gson();
        String jsonStr = gson.toJson(list);
        return jsonStr;
    }
}
